<template>
  <div class="td-registered">
    <el-row>
      <div class="td-form-item">
        <el-col :span="24">
          <el-input
            placeholder="请输入手机号"
            v-model="params.Mobile"
            style="width:100%;"
            prefix-icon="icon font_family icon-td-phone"
          ></el-input>
        </el-col>
      </div>
      <div class="td-form-item">
        <el-col :span="24">
          <el-input
            type="password"
            placeholder="请输入密码"
            v-model="params.Password"
            style="width:100%;"
            prefix-icon="icon font_family icon-td-mima"
          ></el-input>
        </el-col>
      </div>
      <div class="td-form-item">
        <el-col :span="24">
          <el-input
            type="password"
            placeholder="请输入确认密码"
            v-model="params.PasswordTwo"
            style="width:100%;"
            prefix-icon="icon font_family icon-td-mima"
          ></el-input>
        </el-col>
      </div>
      <div class="td-form-item">
        <el-col :span="24">
          <el-input
            placeholder="请输入验证码"
            v-model="params.ValidateCode"
            style="width:100%;"
            prefix-icon="icon font_family icon-td-youxiang"
          ></el-input>
          <el-button
            type="primary"
            class="btnSend"
            size="mini"
            round
            @click="getAuthCode();"
            v-if="sendAuthCode"
          >获取验证码</el-button>
          <el-button
            type="primary"
            class="btnSend"
            size="mini"
            round
            v-if="!sendAuthCode"
          >{{auth_time}}秒后重试</el-button>
        </el-col>
      </div>
      <div
        class="td-form-item"
        :class="{'td-form-patient-label':type===1,'td-form-doctor-label':type===0}"
      >
        <el-col :span="12">
          <router-link to="/tdLogin">用户登录</router-link>
        </el-col>
        <el-col :span="12" style="text-align:right;">
          <router-link to="/tdForget">忘记密码?</router-link>
        </el-col>
      </div>
      <div class="td-form-item">
        <el-col :span="24" style="text-align:center;">
          <el-button
            type="primary" v-if="type===1"
            class="td-btn-patient-primary"
            style="width:80%;"
            round
            @click="btnSubmit();"
          >注册</el-button>

          <el-button
            type="primary" v-if="type===0"
            :class="td-btn-doctor-primary"
            style="width:80%;"
            round
            @click="btnSubmit();"
          >注册</el-button>
        </el-col>
      </div>
    </el-row>
  </div>
</template>

<script>
import AuthService from "../_services/auth.service";
import UserService from "../_services/user.service";
import DoctorService from "../_services/doctor.service";
import HospitalService from "../_services/hospital.service";
import wxService from "../_services/wx.service";
export default {
  name: "TianDingRegisteredComponent",
  data() {
    return {
      params: {
        Mobile: "",
        Password: "",
        PasswordTwo: "",
        ValidateCode: ""
      },
      ValidateCode: "",
      sendAuthCode: true,
      auth_time: 0,
      type: 0
    };
  },
  mounted() {
    this.type = parseInt(this.local$.getItem("tdType"));
  },
  methods: {
    getAuthCode() {
      if (!this.params.Mobile) {
        this.toast$("请输入手机号");
        return;
      }

      this.sendAuthCode = false;
      this.auth_time = 60;

      var auth_timetimer = setInterval(() => {
        this.auth_time--;
        if (this.auth_time <= 0) {
          this.sendAuthCode = true;
          clearInterval(auth_timetimer);
        }
      }, 1000);

      let params = {};
      params.mobile = this.params.Mobile;

      AuthService.sendMsg(params).then(response => {
        if (response && response.Data) {
          if (response.Data.ReturnMsg === "发送成功") {
            this.ValidateCode = response.Data.SendCode;
          }
        }
      });
    },
    btnSubmit() {
      if (!this.params.Mobile) {
        this.toast$("请输入手机号");
        return;
      }

      if (!this.params.Password) {
        this.toast$("请输入密码");
        return;
      }

      if (!this.params.PasswordTwo) {
        this.toast$("请输入确认密码");
        return;
      }

      if (this.params.Password !== this.params.PasswordTwo) {
        this.toast$("两次输入密码不一致");
        return;
      }

      if (
        this.encry$.MD5Encryption(this.params.ValidateCode) !==
        this.ValidateCode
      ) {
        this.toast$("请输入正确的验证码");
        return;
      }

      this.indicator$.open("请稍后...");

      if (this.type === 1) {
        UserService.registerUserInfo(this.params).then(response => {
          let { Data } = response;
          if (!this.utils$.isEmpty(Data.IconPath)) {
            Data.IconPath = require("../assets/images/gril.png");
          }

          this.utils$.setToken(Data.Password);
          this.utils$.setUser(Data);
          this.utils$.setStatus(true);
          this.utils$.setCookie();

          this.toast$("注册成功");
          this.$router.push(`/tdPatient`);
          this.indicator$.close();
        });
      } else {
        DoctorService.registerDoctorInfo(this.params).then(response => {
          let { Data } = response;
          if (!this.utils$.isEmpty(Data.IconPath)) {
            Data.IconPath = require("../assets/images/boy.png");
          }

          this.utils$.setToken(Data.Password);

          let params = {};
          params.doctorID = Data.DoctorID;
          HospitalService.getHospitalListByDoctor(params).then(
            hospitalResponse => {
              Data.hospitalList = hospitalResponse.Data;

              this.utils$.setUser(Data);
              this.utils$.setStatus(true);
              this.utils$.setCookie();

              this.toast$("注册成功");
              this.$router.push(`/tdDoctor`);
              this.indicator$.close();
            }
          );
        });
      }
    }
  }
};
</script>

